/*
* Poppy Game Engine - https://pgetemplate.googlecode.com/svn/
* -----------------------------------------------------------------------
* Licensed under the BSD license, see LICENSE in PSPSDK root for details.
*
* Copyright (c) 2008 Poppy
*/

#pragma once

#include "ThreadLib.h"

namespace PGESystem
{

	enum THREAD_STATE
	{
		THREAD_BEFORE_INIT = 0,
		THREAD_BEFORE_START = 1,
		THREAD_IS_RUNNING = 2,
		THREAD_IS_WAITING = 3,
		THREAD_HAS_EXITED = 4
	};

	class PGESYSTEM_API IThread : private IPGERunnable
	{

	public:

		IThread( LPCSTR name, int size = THREAD_NORMAL, int priority = PGE_THREAD_NORMAL, PGE_Uint attr = PSP_THREAD_ATTR_USER );

		bool InitThread();

		bool StartThread();

		bool GoThread();

		bool ReStartThread();

		virtual int ThreadEntry() = 0;

		bool TerminateThread();

		bool DestoryThread();

		PGE_Uint GetExitStatus();

		bool Wait();

		bool Notify();

		bool SetPriority( int priority );

		bool ExaltPriority( int step = 1 );

		bool DebasePriority( int step = 1 );

		LPCSTR GetThreadName() const;

		int GetStackSize() const;

		int GetPriority() const;

		PGE_Uint GetInitAttr() const;

		int GetThreadState() const;

		virtual ~IThread();

		void Join();

		static void Sleep( PGE_Uint millisecond );

		static void Exit( IThread* thread, PGE_Uint exitcode );

		static PGE_Uint GetId();

		static int GetCurrentPriority();

		static void Jointhread( IThread* thread );

	protected:		

		LPCSTR				m_pThreadName;
		int					m_dStackSize;
		int					m_dPriority;
		PGE_Uint			m_dInitAttr;

	private:

		PGE_THREAD_HANDLE	m_hThreadHandle;
		THREAD_STATE	m_eThreadState;
		LPIPGE_RUNNABLE	m_pPRunnable;

		virtual int Run();

	};

	typedef IThread IThread, *LPIThread;

}

